/**************************************************************************
*   Copyright (C) 2009 Chernov Dmitry                                     *
*   diman4ik.chernov@gmail.com                                            *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "korners_protocol.h"

#include <QPoint>
#include <KDebug>


KornersGameProtocol::KornersGameProtocol( const QString& name, bool server )
{
    m_version = 1;
    m_playerName = name;
    m_server = server;
}

void KornersGameProtocol::processMessage(const QByteArray& message)
{
    int messType = message[0];
    kDebug() << "message " << messType;
    KornersMessage msg;

    switch(messType)
    {
        case KornersMessage::handshake_s :
        case KornersMessage::handshake_c :
		{
		    // Можно ходить
		    msg.type = messType;
                    int protoVer = message[1];
                    if(protoVer == m_version)
                    {
                        QString name = message.right( message.length() - 2 );
                        msg.data.push_back(name);
                        emit(messageParsed(msg));
                    }
                    else
                    {
                        kDebug() << "Network protocol version doesn't match";
                    }
		}
                break;
        case KornersMessage::move :
		{
		    msg.type = KornersMessage::move;
                    QPoint from(message[1], message[2]);
                    QPoint to(message[3], message[4]);
                    msg.data.push_back(from);
                    msg.data.push_back(to);
                    emit(messageParsed(msg));
		}
                break;

        case KornersMessage::regret :
		{
		    msg.type = KornersMessage::regret;
		    emit(messageParsed(msg));
		}
                break;
        default:
            break;
    }
}


const QByteArray& KornersGameProtocol::start()
{
    static QByteArray msg;
    msg.clear();
    if( m_server )
        msg.append( KornersMessage::handshake_s );
    else
        msg.append( KornersMessage::handshake_c );
    msg.append(m_version);
    msg.append( m_playerName.toUtf8() );
    return msg;
}

const QByteArray& KornersGameProtocol::stop()
{
    static QByteArray msg;
    msg.clear();
    msg.append( KornersMessage::regret );
    return msg;
}

const QByteArray& KornersGameProtocol::move( const QPoint& src, const QPoint& dst )
{
    static QByteArray msg;
    msg.clear();
    msg.append( KornersMessage::move );
    msg.append( src.x() );
    msg.append( src.y() );
    msg.append( dst.x() );
    msg.append( dst.y() );
    return msg;
}